<?php
namespace Application\Service;

use Application\Model\Dao\FileDao;
use Application\Model\Dao\UserDao;

class ImageService
{

    private static $IMAGE_DIRECTORY_LOCATION = '/var/fileUpload';

    private static $IMAGE_QUALITY = 85;

    public static $FEED_IMAGE_WIDTH = 75;

    public static $TINY_IMAGE_WIDTH = 30;

    public static $PROFILE_IMAGE_WIDTH = 200;

    private function getImageType($filename)
    {
        return exif_imagetype($filename);
    }


    public function getFace($fileName){
        error_reporting(E_ALL ^ E_NOTICE);
        $faceDetector = new FaceDetector('detection.dat');
         $faceDetector->face_detect($fileName);
        return $faceDetector->toJpeg();
    }

    public function saveAllKindofImage($fileName, $userID)
    {

        $userImage = array();
        $oneImage = null;
        foreach (array(
                     'tiny' => ImageService::$TINY_IMAGE_WIDTH,
                     'feed' => ImageService::$FEED_IMAGE_WIDTH,
                     'profile' => ImageService::$PROFILE_IMAGE_WIDTH
                 ) as $type => $imageWidth) {
            $imageData = $this->getShortenedImageData($fileName, $imageWidth, null);
            $fileDao = new FileDao();
            $id = $fileDao->saveProfileImage('temp', $userID, $imageData);
            $oneImage = array('id' => $id, 'type' => $type);
            $userImage[$type] = $oneImage;
        }
        $xmlw = new XmlService();
        $userImage = $xmlw->processUserXml($userImage , 'user-photo');
        $userDao = new UserDao();
        $userDao->saveProfileImage($userID, $userImage);
    }


    public function getShortenedImageData($orginal_file_pos, $expectedWidth, $originalFileName)
    {
        $data = file_get_contents($orginal_file_pos);
        $im = imagecreatefromstring($data);
        $width = imagesx($im);
        $height = imagesy($im);
        $ratio = ($height / $width);
        $desiredHeight = ($ratio) * $expectedWidth;
        $new = imagecreatetruecolor($expectedWidth, $desiredHeight) or exit("bad url");
        $x = imagesx($im);
        $y = imagesy($im);
        imagecopyresampled($new, $im, 0, 0, 0, 0, $expectedWidth, $desiredHeight, $x, $y) or exit("bad url");
        imagedestroy($im);
        $type = $this->getImageType($orginal_file_pos);
        if ($type == IMAGETYPE_PNG) {
            imagepng($new, ImageService::$IMAGE_DIRECTORY_LOCATION . '/' . $expectedWidth, ImageService::$IMAGE_QUALITY) or exit("bad url");
        } else
            if ($type == IMAGETYPE_JPEG) {
                imagejpeg($new, ImageService::$IMAGE_DIRECTORY_LOCATION . '/' . $expectedWidth, ImageService::$IMAGE_QUALITY) or exit("bad url");
            } else
                if ($type == IMAGETYPE_GIF) {
                    imagegif($new, ImageService::$IMAGE_DIRECTORY_LOCATION . '/' . $expectedWidth, ImageService::$IMAGE_QUALITY) or exit("bad url");
                }
        $data = file_get_contents(ImageService::$IMAGE_DIRECTORY_LOCATION . '/' . $expectedWidth);
        return $data;
    }
}

?>
